﻿using DataLibrary;
using System;
using System.Linq;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_RoleRightAdd : Form
    {
        public Form_RoleRightAdd()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        private void btn_Save_Click(object sender, EventArgs e)
        {
            var count = tse.Roles.Where(u => u.RoleName == this.txt_Role.Text).Count();
            if (count == 0)
            {
                Roles role = new Roles();
                role.RoleName = this.txt_Role.Text;
                tse.Configuration.ValidateOnSaveEnabled = false;
                tse.Roles.Add(role);
                if (tse.SaveChanges() >= 1)
                {
                    RoleRights roleright = new RoleRights();
                    int PhoneId = tse.Rights.Where(u => u.RightName == "显示电话").FirstOrDefault().RightID;
                    int PrintId = tse.Rights.Where(u => u.RightName == "可以打印数据").FirstOrDefault().RightID;
                    int ExportId = tse.Rights.Where(u => u.RightName == "可以导出数据").FirstOrDefault().RightID;
                    int UpdateId = tse.Rights.Where(u => u.RightName == "最高权限").FirstOrDefault().RightID;
                    int RoleId = tse.Roles.Where(u => u.RoleName == this.txt_Role.Text).FirstOrDefault().RoleID;
                    if (this.checkUpdate.Checked)
                    {
                        RoleRights a = new RoleRights();
                        a.RoieID = RoleId;
                        a.RightID = PhoneId;
                        tse.RoleRights.Add(a);
                        RoleRights b = new RoleRights();
                        b.RoieID = RoleId;
                        b.RightID = PrintId;
                        tse.RoleRights.Add(b);
                        RoleRights c = new RoleRights();
                        c.RoieID = RoleId;
                        c.RightID = ExportId;
                        tse.RoleRights.Add(c);
                        RoleRights d = new RoleRights();
                        d.RightID = UpdateId;
                        d.RoieID = RoleId;
                        tse.RoleRights.Add(d);
                    }
                    else
                    {
                        if (this.checkPhone.Checked)
                        {
                            RoleRights a = new RoleRights();
                            a.RoieID = RoleId;
                            a.RightID = PhoneId;
                            tse.RoleRights.Add(a);
                        }
                        if (this.checkPrint.Checked)
                        {
                            RoleRights b = new RoleRights();
                            b.RoieID = RoleId;
                            b.RightID = PrintId;
                            tse.RoleRights.Add(b);
                        }
                        if (this.checkExport.Checked)
                        {
                            RoleRights c = new RoleRights();
                            c.RoieID = RoleId;
                            c.RightID = ExportId;
                            tse.RoleRights.Add(c);
                        }
                    }
                   
                    tse.SaveChanges();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else
            {
                MessageBox.Show("管理员名字重复");
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            var count = tse.Roles.Where(u => u.RoleName == this.txt_Role.Text).Count();
            if (count == 0)
            {
                Roles role = new Roles();
                role.RoleName = this.txt_Role.Text;
                tse.Configuration.ValidateOnSaveEnabled = false;
                tse.Roles.Add(role);
                if (tse.SaveChanges() >= 1)
                {
                    RoleRights roleright = new RoleRights();
                    int PhoneId = tse.Rights.Where(u => u.RightName == "显示电话").FirstOrDefault().RightID;
                    int PrintId = tse.Rights.Where(u => u.RightName == "可以打印数据").FirstOrDefault().RightID;
                    int ExportId = tse.Rights.Where(u => u.RightName == "可以导出数据").FirstOrDefault().RightID;
                    int UpdateId = tse.Rights.Where(u => u.RightName == "最高权限").FirstOrDefault().RightID;
                    int RoleId = tse.Roles.Where(u => u.RoleName == this.txt_Role.Text).FirstOrDefault().RoleID;
                    if (this.checkUpdate.Checked)
                    {
                        RoleRights a = new RoleRights();
                        a.RoieID = RoleId;
                        a.RightID = PhoneId;
                        tse.RoleRights.Add(a);
                        RoleRights b = new RoleRights();
                        b.RoieID = RoleId;
                        b.RightID = PrintId;
                        tse.RoleRights.Add(b);
                        RoleRights c = new RoleRights();
                        c.RoieID = RoleId;
                        c.RightID = ExportId;
                        tse.RoleRights.Add(c);
                        RoleRights d = new RoleRights();
                        d.RightID = UpdateId;
                        d.RoieID = RoleId;
                        tse.RoleRights.Add(d);
                    }
                    else
                    {
                        if (this.checkPhone.Checked)
                        {
                            RoleRights a = new RoleRights();
                            a.RoieID = RoleId;
                            a.RightID = PhoneId;
                            tse.RoleRights.Add(a);
                        }
                        if (this.checkPrint.Checked)
                        {
                            RoleRights b = new RoleRights();
                            b.RoieID = RoleId;
                            b.RightID = PrintId;
                            tse.RoleRights.Add(b);
                        }
                        if (this.checkExport.Checked)
                        {
                            RoleRights c = new RoleRights();
                            c.RoieID = RoleId;
                            c.RightID = ExportId;
                            tse.RoleRights.Add(c);
                        }
                    }
                    tse.SaveChanges();
                    this.txt_Role.Text = null;
                    this.checkPhone.Checked = false;
                    this.checkPrint.Checked = false;
                    this.checkExport.Checked = false;
                    this.checkUpdate.Checked = false;

                }
                else
                {
                    MessageBox.Show("添加失败了");
                }

            }
            else
            {
                MessageBox.Show("管理员名字重复了");
            }          
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkUpdate_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkUpdate.Checked == true)
            {
                this.checkPhone.Checked = true;
                this.checkPhone.Enabled = false;
                this.checkPrint.Checked = true;
                this.checkPrint.Enabled = false;
                this.checkExport.Checked = true;
                this.checkExport.Enabled = false;
            }
            if (this.checkUpdate.Checked == false)
            {
                this.checkPhone.Enabled = true;
                this.checkPrint.Enabled = true;
                this.checkExport.Enabled = true;
                this.checkPhone.Checked = false;
                this.checkPrint.Checked = false;
                this.checkExport.Checked = false;
            }
        }
    }
}
